/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.handler.invoke;

import org.apache.log4j.Logger;

import cn.tworen.demou.cmd.CmdName;
import cn.tworen.demou.cmd.IInvokeCmd;
import cn.tworen.demou.cmd.InvokeCmd;
import cn.tworen.demou.io.AsBean;
import cn.tworen.demou.rtmp.Client;
import cn.tworen.demou.rtmp.NCInfo;
import cn.tworen.demou.rtmp.NSInfo;
import cn.tworen.demou.stream.Stream;

/**
 * Created on  2007-1-20
 *
 * Title       : Publish.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-1-20
 * History     : 
 * 
 */
public class Publish implements IInvoke {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Publish.class);

	public void invoke(Client client, IInvokeCmd invokeCmd) {
		logger.debug("Publish()");
		Stream stream = client.getStream(invokeCmd.getHeader().getSource());
		if (stream != null) {
			stream.setChunkIndex(invokeCmd.getHeader().getChunkIndex());
			stream.setStreamId(invokeCmd.getHeader().getSource());
			Object[] args = null;
			if (invokeCmd.getParams().size() > 0) {
				args = new Object[invokeCmd.getParams().size()];
				for(int i = 0; i < invokeCmd.getParams().size(); i ++) {
					args[i] = invokeCmd.getParams().get(i);
				}
			} else {
				args = new Object[0];
			}
			this.publishPolicy(stream, args);
		} else {
			this.streamFialed(client, invokeCmd);
		}
	}
	public void publishPolicy(Stream stream, Object[] argvs) {
		String name = argvs[0].toString();
		boolean record = false;
		boolean append = false;
		if(argvs.length > 1){
			String model = argvs[1].toString();
			logger.debug("model=" + model);
			if ("live".equalsIgnoreCase(model)) {
				record = false;
				append = false;
			} else if ("record".equalsIgnoreCase(model)){
				record = true;
				append = false;
			} else if ("append".equalsIgnoreCase(model)) {
				record = true;
				append = true;
			}
		}
		if (record && !append) {
			stream.publishAndRecord(name);
		} else if (!record && append) {
			stream.publishAndAppend(name);
		} else {
			stream.publish(name);
		}
	}
	protected void streamFialed(Client client, IInvokeCmd invokeCmd) {
		IInvokeCmd cmdOut = new InvokeCmd(client.getInvokeDataType());
		cmdOut.setHeader(invokeCmd.getHeader().clone());
		cmdOut.setCmdName(CmdName._result);
		cmdOut.setTrxId(invokeCmd.getTrxId());
		AsBean<String, Object> info = new AsBean<String, Object>();
		info.put(NCInfo.INFO_LEVEL, NCInfo.INFO_VALUE_ERROR);
		info.put(NCInfo.INFO_CODE, NSInfo.NS_FIALED);
		info.put(NCInfo.INFO_DESCRIPTION, "Target Stream object no init!");
		info.put(NCInfo.INFO_DETAILS, null);
		info.put(NCInfo.INFO_OBJECT_ENCODING, client.getObjectEncoding());
		cmdOut.addParam(info);
		client.getRoutePacketOut().route(cmdOut);
	}
}
